package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.LisenceAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.CheckModel;
import com.pengcheng.taskapplication.model.SubscribeOrderListModel;
import com.pengcheng.taskapplication.model.UserDetailVo;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.view.widget.MyListView;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.rxbus.RxBusRoute;
import com.pengcheng.taskapplication.rxbus.RxEvent;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZFT on 2020/10/24.
 */

public class ApplyListActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_center_title)
    TextView textView;
    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshListView myListView;
    private int page=1;
    private int size=100;

    private UserPresenter userPresenter;
    ArrayList<SubscribeOrderListModel> listModelArrayList=new ArrayList<>();

    UserDetailVo userDetailVo;
    private LisenceAdapter adapter;
    @Override
    protected int layoutResID() {
        return R.layout.activity_apply_list_layout;
    }

    @Override
    protected void initData() {
        textView.setText("我的申请");
        userPresenter= new UserPresenter(this);
        adapter=new LisenceAdapter(context,listModelArrayList);
        myListView.setAdapter(adapter);
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                SubscribeOrderListModel  model =(SubscribeOrderListModel) adapterView.getAdapter().getItem(i);
                Intent intent =new Intent(context,ApplySuccessActivity.class);
                intent.putExtra("code",model.getCode());
                startActivity(intent);
            }
        });
        myListView.setEmptyView(findViewById(R.id.empty_view));
        getSubscribes(page,size);
    }
    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.getSubscribes)){
            listModelArrayList.clear();
            listModelArrayList.addAll((ArrayList<SubscribeOrderListModel>)data);
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToast(msg);
    }

    @OnClick({R.id.img_back,R.id.ll_blcp})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_blcp:


                boolean isreal=SPUtils.getBoolean(context, ConstantValue.SpType.isreal);

                if(isreal) {
                    Intent intent = new Intent(context, Html5AgressActivity.class);
                    intent.putExtra("url", "file:///android_asset/instructions.html");
                    intent.putExtra("title", "办理须知");
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(context, UploadIdCard.class);
                    startActivity(intent);
                }

                break;
        }
    }
    public void checkOcr(){
        userPresenter.checkCert(ConstantValue.RequestKey.checkCert);
    }

    public void getUserDetail(){
        userPresenter.getUserDetail(ConstantValue.RequestKey.userinfo);
    }
    public void getSubscribes(int page,int pageSize){
        userPresenter.getSubscribes(page,pageSize,ConstantValue.RequestKey.getSubscribes);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected boolean isRegistRxBus() {
        return true;
    }

    /**
     * 注册订阅事件
     */
    protected void receiveEvent(RxEvent rxEvent) {
        switch (rxEvent.busName) {
            case RxBusRoute.REFRESH:
                page = 1;
                listModelArrayList.clear();
                getSubscribes(page, size);
                break;
        }
    }
}
